// sharing/pages/user/user.js
var app = getApp();
Page({
	TOAPPID: require('../../resource/components/footer/footer.js'),
  /**
   * 页面的初始数据
   */
  data: {
	  res: app.siteInfo.resource,
	  user: [],
	  DuoguanForm: 1,
  },
  onShow: function(){
	  var isSet = wx.getStorageSync('setUser');
	  if(isSet==1){
		  this.onLoad();
		  wx.removeStorageSync('setUser');
	  }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	  var that = this;
	  app.footerInit(that);
	  app.getShareData(function(res){that.setData({cfg: res.data})},2);
    app.getUserInfo(function(openid){
      wx.getUserInfo({
        success: function (wxInfo) {
          var info = JSON.parse(wxInfo.rawData);
          app.util.request({
            url: 'entry/wxapp/updateUser',
            data: { openid: openid, avatar: info.avatarUrl, nickname: info.nickName, gender: info.gender, m: 'junsion_wifinav'},
            cachetime: 0,
            success: function (res) {
              that.getUser(openid);
            }
          });
        },
        fail: function(){
          that.getUser(openid);
        }
      });
    });
  },
  getUser: function (openid){
    var that = this;
    app.util.request({
      url: 'entry/wxapp/getUser',
      data: { openid: openid, m: 'junsion_wifinav'},
      cachetime: 0,
      success: function (res) {
        that.setData({user: res.data});
      }
    }); 
  },
  navigator: function (e){
	  wx.navigateTo({url: e.currentTarget.dataset.url});
  },
  DuoguanForm: function (){
	  this.setData({DuoguanForm: !this.data.DuoguanForm});
  },
  onShareAppMessage: function () {
	  return {
	      title: this.data.cfg.share.userShare,
	      path: '/sharing/pages/user/user',
	      imageUrl: this.data.cfg.share.userPic,
	      success: function(res) {
	        // 转发成功
	      },
	      fail: function(res) {
	        // 转发失败
	      }
	    }
  },
  onPullDownRefresh: function(){
	  this.onLoad();
	  wx.stopPullDownRefresh();
  },
  powerDrawer: function (e) {
    var currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu)
  },
  util: function (currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例 
    var animation = wx.createAnimation({
      duration: 200, //动画时长 
      timingFunction: "linear", //线性 
      delay: 0 //0则不延迟 
    });

    // 第2步：这个动画实例赋给当前的动画实例 
    this.animation = animation;

    // 第3步：执行第一组动画 
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存 
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画 
    setTimeout(function () {
      // 执行第二组动画 
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象 
      this.setData({
        animationData: animation
      })

      //关闭 
      if (currentStatu == "close") {
        this.setData(
          {
            showModalStatus: false
          }
        );
      }
    }.bind(this), 200)

    // 显示 
    if (currentStatu == "open") {
      this.setData(
        {
          showModalStatus: true
        }
      );
    }
    
    // 确定 
    if (currentStatu == "confrim") {
    	this.setData({
			showModalStatus: false
		});
    	var user = this.data.user;
    	if(!user.mobile || !user.address){
    		wx.showModal({
    			title: '温馨提示',
    			content: '请完善会员信息',
    			showCancel: false,
    			success: function(){
    				wx.navigateTo({url: './useredit/index'});
    			}
    		});
    		return ;
    	}
    	
    	var that = this;
        app.util.request({
          url: 'entry/wxapp/appDistributor',
          data: { uid: user.id, m: 'junsion_wifinav'},
          cachetime: 0,
          success: function (res) {
        	  console.log(res)
            if(res.data.code==1){
            	wx.showModal({
        			title: '温馨提示',
        			content: res.data.msg,
        			showCancel: false,
        			success: function(){
        				that.getUser(user.openid);
        			}
        		});
            }else if(res.data.code==2){
            	wx.requestPayment({
                    timeStamp: res.data.params.timeStamp,
                    nonceStr: res.data.params.nonceStr,
                    package: res.data.params.package,
                    signType: res.data.params.signType,
                    paySign: res.data.params.paySign,
                    success: function (r) {
                      wx.showModal({
                        title: '温馨提示',
                        content: res.data.msg,
                        showCancel: false,
                        success: function (r2) {
                          if (r2.confirm) {
                        	 that.getUser(user.openid);
                          }
                        }
                      });
                    }
                  });
            }else{
            	wx.showModal({
        			title: '温馨提示',
        			content: res.data.msg,
        			showCancel: false,
        			success: function(){
        				
        			}
        		});
            }
          }
        }); 
    }
  }
})